package com.leancloud.login.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.leancloud.login.R;
import com.leancloud.login.user.LoginBack;
import com.leancloud.login.user.UserGlobal;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.event.Subscribe;

import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.manager.GlideManager;
import tech.com.commoncore.user.CurrentUser;

import static com.leancloud.login.LoginPath.LOGIN_ABOUT;
import static com.leancloud.login.LoginPath.LOGIN_FEEDBACK;
import static com.leancloud.login.LoginPath.LOGIN_NOTIFICATION;
import static com.leancloud.login.LoginPath.LOGIN_SETTINGS;
import static com.leancloud.login.LoginPath.LOGIN_SETTINGSME;

public class PersonalFragment extends BaseFragment implements View.OnClickListener {
    private LinearLayout personalLayout;
    private ImageView headImg;
    private TextView nikeTx, signTx, logoutTx, fansTx, attentionTx, articleTx;

    public static PersonalFragment newInstance() {
        Bundle args = new Bundle();
        PersonalFragment fragment = new PersonalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragmnet_personal;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        personalLayout = mContentView.findViewById(R.id.persona_layout);
        headImg = mContentView.findViewById(R.id.persona_head_img);
        nikeTx = mContentView.findViewById(R.id.persona_nike_tx);
        signTx = mContentView.findViewById(R.id.persona_sign_tx);
        logoutTx = mContentView.findViewById(R.id.logout_tx);

        headImg.setOnClickListener(this);
        nikeTx.setOnClickListener(this);
        signTx.setOnClickListener(this);
        logoutTx.setOnClickListener(this);
        mContentView.findViewById(R.id.persona_item_1).setOnClickListener(this);
        mContentView.findViewById(R.id.persona_item_2).setOnClickListener(this);
        mContentView.findViewById(R.id.persona_item_3).setOnClickListener(this);
        mContentView.findViewById(R.id.persona_item_4).setOnClickListener(this);
        mContentView.findViewById(R.id.persona_item_5).setOnClickListener(this);

        mContentView.findViewById(R.id.persona_option_1).setOnClickListener(this);
        mContentView.findViewById(R.id.persona_option_2).setOnClickListener(this);

        setUserInfo();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.persona_head_img || v.getId() == R.id.persona_nike_tx || v.getId() == R.id.persona_sign_tx)
            ARouter.getInstance().build(LOGIN_SETTINGSME).navigation();
        if (v.getId() == R.id.persona_item_1)
//            ARouter.getInstance().build(CURRENCY_OPTIONAL_SET).greenChannel().navigation();
        if (v.getId() == R.id.persona_item_2)
//            ARouter.getInstance().build(CIRCLE_MY_COLLECTION).navigation();
        if (v.getId() == R.id.persona_item_3)
            ARouter.getInstance().build(LOGIN_FEEDBACK).greenChannel().navigation();
        if (v.getId() == R.id.persona_item_4)
            ARouter.getInstance().build(LOGIN_SETTINGS).greenChannel().navigation();
        if (v.getId() == R.id.persona_item_5)
            ARouter.getInstance().build(LOGIN_ABOUT).greenChannel().navigation();
        if (v.getId() == R.id.logout_tx)
            logout();
        if (v.getId() == R.id.persona_option_1)
//            ARouter.getInstance().build(CIRCLE_MY_ARTICLE).navigation();
        if (v.getId() == R.id.persona_option_2)
//            ARouter.getInstance().build(CIRCLE_MY_ATTENTION).navigation();
        if (v.getId() == R.id.persona_notification_tx)
            ARouter.getInstance().build(LOGIN_NOTIFICATION).navigation();
    }

    private void logout() {
        UserGlobal.getUserImp().logout(new LoginBack() {
            @Override
            public void onBack(Object o, String err) {
                if (err == null) {
                    setUserInfo();
                }
            }
        });
        BusManager.getBus().post(new LoginChangeEvent(false));
    }

    private void setUserInfo() {
        CurrentUser user = UserGlobal.getUserImp().getCurrentUser();
        String name = "立即登录";
        String number = "";
        String headUrl = "";
        String nikeName = "立即登录";
        String sign = "您还没有登录哦";

        logoutTx.setVisibility(View.GONE);

        if (user != null) {
            name = user.getNickName();
            number = user.getMobile();
            headUrl = user.getAvatar();
            nikeName = user.getNickName();
            nikeName = (nikeName == null || nikeName.isEmpty()) ? "未设置昵称" : nikeName;
            sign = user.getSignature();
            sign = (sign == null || sign.isEmpty()) ? "这个家伙很懒,什么也没留下" : sign;

            logoutTx.setVisibility(View.VISIBLE);
        }
        GlideManager.loadCircleImg(headUrl, headImg, R.mipmap.icon_default_head);
        nikeTx.setText(nikeName);
        signTx.setText(sign);
    }

    @Subscribe
    public void userUpdateEvent(LoginChangeEvent event) {
        setUserInfo();
    }

}
